# - Coding UTF8 -
#
# Networked Decision Making
# Site: http://code.google.com/p/global-decision-making-system/
#
# License Code: GPL, General Public License v. 2.0
# License Content: Creative Commons Attribution 3.0
#
# Also visit: www.web2py.com
#             or Groups: http://groups.google.com/group/web2py 
# 	For details on the web framework used for this development
#
# Developed by Russ King (newglobalstrategy@gmail.com
# Russ also blogs occasionally to pass the time at: 
# http://proudofyourplanent.blogspot.com
# His general thinking on why this project is very important is available at
# http://www.scribd.com/doc/98216626/New-Global-Strategy
# With thanks to Guido, Massimo and many other that make this sort of thing
# much easier than it used to be


def index():
    '''
    This is the startup function.
    
    It retrieve the 5 highest priority actions, 5 most recently resolved quests
    and highest priority quests in progress.
        
    For actions - any status except rejected are wanted but to avoid an or or a
    not for GAE we will use ans3 for this purpose with numans always two for an 
    action this is ok.  All quereis are cached for 2 mins which should be OK
    '''

    response.flash = "Welcome to Net Decision Making"
    subj = db(db.subject.id>0).select(db.subject.longdesc).first()
    if subj is None:
        redirect(URL('admin','init'))

    # retrieve the top 5 actions by priority regardless of status
    actions = db(db.question.qtype == 'action').select(
                 db.question.id,db.question.status, db.question.questiontext, 
                 db.question.responsible, db.question.scopetext,
                 db.question.duedate, db.question.category,
                 orderby=~db.question.priority,limitby=(0,5),
                 cache=(cache.ram,600), cacheable=True) 

    # retrieve the top 5 questions in progresss by priority
    quests = db((db.question.qtype == 'quest') & (db.question.status == 'In Progress')).select(
                db.question.id,db.question.questiontext,db.question.level,
                db.question.priority,
                orderby=~db.question.priority,limitby=(0,5),
                cache=(cache.ram,600), cacheable=True)

    # retrieve the 5 most recently resolved questions
    answers = db((db.question.qtype == 'quest') & (db.question.status == 'Resolved')).select(
                 db.question.id,db.question.questiontext, 
                 db.question.correctanstext,db.question.resolvedate,
                 db.question.numagree, db.question.numdisagree,db.question.level,
                 orderby=~db.question.priority,limitby=(0,5),
                 cache=(cache.ram,600), cacheable=True)    
    
    return locals()

def user():
    """
    exposes:
    http://..../[app]/default/user/login
    http://..../[app]/default/user/logout
    http://..../[app]/default/user/register
    http://..../[app]/default/user/profile
    http://..../[app]/default/user/retrieve_password
    http://..../[app]/default/user/change_password
    use @auth.requires_login()
        @auth.requires_membership('group name')
        @auth.requires_permission('read','table name',record_id)
    to decorate functions that need access control
    """

    if request.args[0] == "register":
        response.title = "Net Decision Making: Registration"

    session.exclude_cats = None
    session.comblist = None
    session.questlist = None
    session.actlist = None
    session.continent = None
    session.country = None
    session.subdivision = None

    return dict(form=auth())


def call():
    """
    exposes services. for example:
    http://..../[app]/default/call/jsonrpc
    decorate with @services.jsonrpc the functions to expose
    supports xml, json, xmlrpc, jsonrpc, amfrpc, rss, csv
    """
    return service()


@auth.requires_signature()
def data():
    """
    http://..../[app]/default/data/tables
    http://..../[app]/default/data/create/[table]
    http://..../[app]/default/data/read/[table]/[id]
    http://..../[app]/default/data/update/[table]/[id]
    http://..../[app]/default/data/delete/[table]/[id]
    http://..../[app]/default/data/select/[table]
    http://..../[app]/default/data/search/[table]
    but URLs must be signed, i.e. linked with
      A('table',_href=URL('data/tables',user_signature=True))
    or with the signed load operator
      LOAD('default','data.load',args='tables',ajax=True,user_signature=True)
    """
    return dict(form=crud())
